import api from '../../utils/api';
import { QueryType, CommentType } from '../../types/index'

Page({
  data: {
    list: []
  },
  onLoad(query: QueryType) {
    this.getList(query?.userId).then(res => {
      this.setData({
        list: res
      })
    })
  },
  // 获取当前用户的评论列表
  getList (userId) {
    return api.getComments({author: userId}, true).then((res: CommentType[]) => {
      // 去掉重复的文章 id
      const uniqueArray = res.filter((item, index, self) =>
        index === self.findIndex((t) => (t.post === item.post))
      );
      // 获取文章信息
      const articleList = uniqueArray.map(item => {
        return api.getArticlebyID(item.post).then(article => {
          return article;
        })
      })
      return Promise.all(articleList);
    })
  },
});
